package com.ruoyi.web.controller.V2.web.conntroller.font;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.recycle.service.IKrecycleService;
import com.ruoyi.system.V2.tool.service.IBacklistService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.model.domain.Backlist;
import com.ruoyi.system.model.domain.Krecycle;
import com.ruoyi.system.model.dto.H5BacklistReq;
import com.ruoyi.system.sms.service.ISendSms;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * web 我的收藏 功能
 *
 * @author zyy
 * @date 2023-05-03
 */
@RestController
@RequestMapping("/h5/goods/back")
public class H5BackController extends BaseController {

    @Autowired
    private IBacklistService backlistService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IKrecycleService krecycleService;
    @Autowired
    private ISendSms sendSms;
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
//    /**
//     *
//     */
//    @GetMapping("/list")
//    public TableDataInfo list(Backlist backlist) {
//        startPage();
//        List<Backlist> list = backlistService.selectBacklistList(backlist);
//        return getDataTable(list);
//    }
//
//
//    /**
//     * 我的账号
//     */
//    @GetMapping("/allList")
//    public TableDataInfo allList(Backlist backlist) {
//        startPage();
//        List<Backlist> list = backlistService.allList(backlist);
//        return getDataTable(list);
//    }


//    /**
//     *
//     */
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return AjaxResult.success(backlistService.selectBacklistById(id));
//    }

    /**
     * @param tel
     * @return
     */
    @GetMapping(value = "/listForTel")
    public TableDataInfo listForTel(String tel) {
        if (StringUtils.isEmpty(tel)) {
            throw new GlobalException("用户手机号不能为空");
        }
        List<Backlist> list = backlistService.selectListH5ForTel(tel);
        return getDataTable(list);
    }

    /**
     *
     */
    @PostMapping
    public AjaxResult add(@RequestBody H5BacklistReq h5BacklistReq) {
        if (StringUtils.isEmpty(h5BacklistReq.getDxyzm())) {
            throw new GlobalException("短信验证码不能为空");
        }
        if (StringUtils.isEmpty(h5BacklistReq.getTel())) {
            throw new GlobalException("用户手机号不能为空");
        }
        if (Objects.isNull(h5BacklistReq.getSupplyUserId())) {
            throw new GlobalException("商户id不能为空");
        }
        if (Objects.isNull(h5BacklistReq.getYzmRankey())) {
            throw new GlobalException("验证码无效");
        }
        boolean flag = checkYZM(h5BacklistReq.getDxyzm(), h5BacklistReq.getYzmRankey());
        if (!flag) {
            throw new GlobalException("验证码无效");
        }
        Backlist backlist = new Backlist();
        BeanUtils.copyProperties(h5BacklistReq, backlist);
        backlistService.insertH5Backlist(backlist);
        redisCache.deleteObject(h5BacklistReq.getYzmRankey());
        //配置字典后才会发短信
        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType("publishSuccess");
        if (CollectionUtils.isNotEmpty(sysDictData)) {
            SysDictData s = sysDictData.get(0);
            if (Objects.nonNull(s) && StringUtils.equals(s.getDictValue(), "on")) {
                try {
                    sendSms.sendSmsForNotice(h5BacklistReq.getTel(), "huishou");
                } catch (Exception e) {
                    System.out.println("短信发送失败" + e.getMessage());
                }
            }
        }
        return AjaxResult.success(backlist);
    }

//    /**
//     *
//     */
//    @PutMapping
//    public AjaxResult edit(@RequestBody Backlist backlist) {
//        return toAjax(backlistService.updateBacklist(backlist));
//    }
//
//    /**
//     *
//     */
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(backlistService.deleteBacklistByIds(ids));
//    }

    @PostMapping(value = "/saveKrecycle")
    public AjaxResult saveKrecycle(@RequestBody Krecycle krecycle) {

        return toAjax(krecycleService.insertKrecycle(krecycle));
    }

    /**
     * 验证 传进来的验证码与缓存中的验证码是否相等
     *
     * @param yzm      前端输入的验证码
     * @param cacheKey 缓存中取验证码的key
     */
    public boolean checkYZM(String yzm, String cacheKey) {
        boolean flag = false;
        String cacheyzm = redisCache.getCacheObject(cacheKey);
        if (Objects.nonNull(cacheyzm)) {
            if (Objects.equals(yzm, cacheyzm)) {
                flag = true;
            }
        }
        return flag;
    }
}


